package com.scraper.articles;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * Base scraper implementation.
 *
 * @param <T> result object type that will be populated by scanner.
 */
public abstract class BaseScraper<T> implements Scraper<T> {

    private final Element element;

    public BaseScraper(Element element) {
        this.element = element;
    }

    @Override
    public T call() throws Exception {
        Document document = getDocument(element);
        return parseDocument(document);
    }

    /**
     * Scraping data from Jsoup {@link org.jsoup.nodes.Document}.
     *
     * @param document Jsoup {@link org.jsoup.nodes.Document} to scrap data from.
     * @return scraped data object.
     */
    protected abstract T parseDocument(Document document);

    /**
     * Gets Jsoup {@link org.jsoup.nodes.Document} from {@link org.jsoup.nodes.Element} data.
     *
     * @param element Jsoup {@link org.jsoup.nodes.Element} with specific page link.
     * @return Jsoup {@link org.jsoup.nodes.Document} representation of page.
     */
    protected abstract Document getDocument(Element element);
}
